// 导入webpack插件，不需要安装，vue-cli里面已经集成好了
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')

const isProd = process.env.NODE_ENV === 'production'

let plugins = []
let externals = {}
let cdn = { js: [], css: [] }
if (isProd) {
  plugins = [
    new BundleAnalyzerPlugin({
      analyzerPort: 9999 // 端口号
    })
  ]
  externals = {
    vue: 'Vue',
    'element-ui': 'ELEMENT',
    'vue-router': 'VueRouter',
    axios: 'axios',
    lodash: '_',
    echarts: 'echarts',
    nprogress: 'NProgress',
    'vue-quill-editor': 'VueQuillEditor'
  }
  cdn = {
    css: [
      'https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.css',
      'https://cdn.staticfile.org/quill/1.3.4/quill.core.min.css',
      'https://cdn.staticfile.org/quill/1.3.4/quill.snow.min.css',
      'https://cdn.staticfile.org/quill/1.3.4/quill.bubble.min.css',
      'https://cdn.staticfile.org/element-ui/2.8.2/theme-chalk/index.css'
    ],
    js: [
      'https://cdn.staticfile.org/vue/2.5.22/vue.min.js',
      'https://cdn.staticfile.org/vue-router/3.0.1/vue-router.min.js',
      'https://cdn.staticfile.org/axios/0.18.0/axios.min.js',
      'https://cdn.staticfile.org/lodash.js/4.17.11/lodash.min.js',
      'https://cdn.staticfile.org/echarts/4.1.0/echarts.min.js',
      'https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.js',
      'https://cdn.staticfile.org/quill/1.3.4/quill.min.js',
      'https://cdn.jsdelivr.net/npm/vue-quill-editor@3.0.4/dist/vue-quill-editor.js',
      'https://cdn.staticfile.org/element-ui/2.8.2/index.js'
    ]
  }
}

module.exports = {
  publicPath: isProd ? './' : '/',
  outputDir: 'dist',
  assetsDir: 'static',
  indexPath: 'index.html',
  devServer: {
    host: '127.0.0.1',
    port: 6666
    // open: true,
    // proxy:{ // 设置跨域代理 }
  },
  configureWebpack: {
    plugins,
    module: {},
    externals
  },
  chainWebpack: config => {
    // 配置 webpack-plugin-html 插件中的内容。 args[0] 表示配置信息，增加cdn变量
    config.plugin('html').tap(args => {
      args[0].cdn = cdn
      return args // 需要return
    })
  }
}
